package com.carleasoft.mps.cms.business.train.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.cms.business.train.bean.entity.TrainPlan;
import com.carleasoft.mps.cms.business.train.bean.entity.TrainPlanType;
import com.carleasoft.mps.cms.business.train.constants.ExceptionTrainEnum;
import com.carleasoft.mps.cms.business.train.dao.TrainPlanTypeMapper;
import com.carleasoft.mps.cms.business.train.service.TrainPlanService;
import com.carleasoft.mps.cms.business.train.service.TrainPlanTypeService;
import com.carleasoft.mps.core.exceptions.BusinessException;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.user.UserCacheData;
import com.carleasoft.mps.core.utils.IBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * <p>
 * 训练计划类型表 服务实现类
 * </p>
 *
 * @author fan
 * @since 2023-06-09
 */
@Service
public class TrainPlanTypeServiceImpl extends ServiceImpl<TrainPlanTypeMapper, TrainPlanType> implements TrainPlanTypeService {

    @Autowired
    TrainPlanService trainPlanService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(TrainPlanType trainPlanType) {
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        trainPlanType.setTypeCode(UUID.randomUUID().toString().replaceAll("-",""));
        trainPlanType.setTheSort(0);
        trainPlanType.setCreatePersonCode(user.getUserCode());
        trainPlanType.setCreatePersonName(user.getUserName());
        trainPlanType.setCreateTime(LocalDateTime.now());
        trainPlanType.setUpdatePersonCode(user.getUserCode());
        trainPlanType.setUpdatePersonName(user.getUserName());
        trainPlanType.setUpdateTime(LocalDateTime.now());
        return this.save(trainPlanType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean edit(TrainPlanType trainPlanType) {
        IBeanUtil.paramVerify(trainPlanType,"typeCode");
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        trainPlanType.setUpdatePersonCode(user.getUserCode());
        trainPlanType.setUpdatePersonName(user.getUserName());
        trainPlanType.setUpdateTime(LocalDateTime.now());
        LambdaQueryWrapper<TrainPlanType> eq = Wrappers.lambdaQuery(TrainPlanType.class).eq(TrainPlanType::getTypeCode, trainPlanType.getTypeCode());
        return this.update(trainPlanType,eq);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(TrainPlanType trainPlanType) {
        IBeanUtil.paramVerify(trainPlanType,"typeCode");
        //校验类型是否被使用
        int num = trainPlanService.count(new QueryWrapper<TrainPlan>().eq("PLAN_CODE", trainPlanType.getTypeCode()));
        if (num>0){
            throw new BusinessException(ExceptionTrainEnum.PLAN_TYPE_USED_ERROR);
        }
        return this.remove(new QueryWrapper<TrainPlanType>().eq("PLAN_CODE", trainPlanType.getTypeCode()));
    }
}
